﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Store.Manager.ERP.Application.Dtos;
using Store.Manager.ERP.Application.Services;
using Store.Manager.ERP.Common.Models;
using static Dm.net.buffer.ByteArrayBuffer;

namespace StoreManager.ERP.Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class OrganizationController : ControllerBase
    {
        private readonly IOrganizationService _organizationService;

        public OrganizationController(IOrganizationService organizationService)
        {
            _organizationService = organizationService;
        }

        /// <summary>
        /// 添加组织
        /// </summary>
        /// <param name="organizationDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<CommonResponse<OrganizationDto>> AddOrganization([FromBody] OrganizationDto organizationDto)
        {
            var serviceRes = await _organizationService.Add(organizationDto);
            return serviceRes;
        }

        /// <summary>
        /// 批量添加组织
        /// </summary>
        /// <param name="organizationDtos"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> AddOrganizations([FromBody] List<OrganizationDto> organizationDtos)
        {
            var result = await _organizationService.AddOrganizations(organizationDtos);
            return result ? Ok("添加成功"):BadRequest("添加失败");
        }
        /// <summary>
        /// 更新组织
        /// </summary>
        /// <param name="organizationDto"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> UpdateOrganization([FromBody] OrganizationDto organizationDto)
        {
            await _organizationService.Update(organizationDto);
            return Ok();
        }

        /// <summary>
        /// 删除组织
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{code}")]
        public async Task<IActionResult> DeleteOrganization(string code)
        {
            await _organizationService.Delete(code);
            return Ok();
        }

        /// <summary>
        /// 根据 ID 获取组织
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{code}")]
        public async Task<IActionResult> GetOrganizationById(string code)
        {
            var organizationDto = await _organizationService.GetById(code);
            if (organizationDto == null)
            {
                return NotFound();
            }
            return Ok(organizationDto);
        }

        /// <summary>
        /// 获取所有组织
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetAllOrganizations()
        {
            var organizations = await _organizationService.GetAll();
            return Ok(organizations);
        }
    }
}
